import datetime
import requests


class WxRobot:
    def __init__(self):
        self.allure = "http://admin:Aa123456!@47.93.123.21:8080/job/abc/allure/widgets/suites.json"
        self.wx = 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=f6ba093b-f129-4a90-96be-5cb11cb58187'
        self.error = self.get_allure_error()

    def get_allure_error(self):
        jenkins_data = requests.get(self.allure).json()
        case_error = jenkins_data["items"][0]["statistic"]["failed"] + jenkins_data["items"][0]["statistic"]["broken"]
        return case_error

    def send_report(self):
        if self.error > 0:
            headers = {"Content-Type": "application/json;charset=utf-8"}
            body = {
                "msgtype": "text",
                "text": {
                    "content": datetime.datetime.now().strftime(
                        '%Y-%m-%d %H:%M:%S') + "   接口自动化框架运行失败\n" + "http://admin:Aa123456!@47.93.123.21:8080/job/abc/allure",
                    "mentioned_mobile_list": ["18141200880"]
                }
            }
            response = requests.post(self.wx, headers=headers, json=body)

        else:
            print('无报错')


if __name__ == '__main__':
    d = WxRobot()
    print(d.send_report())

